<div id="queue-box">
  <% form_remote_tag( :url => users_path, :method => :post ) do %>
    your name (to credit your queuings):<br/>
    <input type="text" name="username" value="<%= session[:username] %>"/>
    <input type="submit" value="set"/>
  <% end %>
  <p>
  logged in as: <%= session[:username].blank? ? "anonymous" : session[:username] %><br/>
  status: iTunes is <%= @state %> <br/>
  <% if @state != 'stopped' %>
    current playlisting playlist: <%= @iTunes.currentPlaylist.name %>
  <% end %>
  <% if @state == 'stopped' || @iTunes.currentPlaylist.name.strip != ITunes::QUEUE_PLAYLIST %>
    <br/><%= link_to_remote "start playing the queue (#{ITunes::QUEUE_PLAYLIST})", :url => {:controller => "queued_tracks", :action => "start_queue"},
    :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
  <% end %>
  </p>
  <div>
    <% form_remote_tag(:url => url_for({:controller => "queued_tracks", :action => :change_volume}), :method => :get ) do %>
    volume: 
      <%= text_field_tag "volume", @iTunes.soundVolume, {:size => 3} %>
      <%= submit_tag "change" %>
    <% end %>
  </div>

  <br/>
  <%= link_to_remote "skip to next", :url => {:controller => "queued_tracks", :action => "skip_current"},
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()" %> 
  / <%= link_to_remote "pause/play", :url => {:controller => "queued_tracks", :action => "playpause"},
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
  / <%= link_to_remote "previous track", :url => {:controller => "queued_tracks", :action => "previous_track"},
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
  <p><%= link_to_remote "refresh queue info", :url => {:controller => "queued_tracks", :action => "reload_queue_box" },
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
  / <%= link_to_remote "toggle shuffle mode", :url => {:controller => "queued_tracks", :action => "toggle_shuffle" },
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>

  <% if @state != "stopped" and @current_track %>
  <h2>currently playing</h2>
  <%= album_art_image_tag unless @state == "stopped" %>
  <p><%= @current_track.name %>
  / <%= @current_track.artist %>
  / <%= @current_track.album %></p>
  <script>
    jQuery(function() {
      jQuery('title').html("iTunes Rails :: <%= h @current_track.artist.gsub("'", "\\'") %> :: <%= h @current_track.name.gsub("'", "\\'") %>"); });
  </script>
  <% else %>
  <script>
    jQuery('title').html('iTunes Rails');
  </script>
  <% end %>

  <h2>queue
    <% if @iTunes.queue.shuffle == 1 %>
    <small>(in shuffle mode)</small>
    <% end %>
  </h2>
    
  <% if @current_track_index == 0 %>
    <p>The playlist is finished playing</p>
  <% end %>
  <% if @queued_tracks.size > 0 %>
    <p>
    <%= link_to_remote "clear the queue", :url => {:controller => "queued_tracks", :action => "clear" },
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()" %>
    / <%= link_to_remote "clear upcoming tracks", :url => {:controller => "queued_tracks", :action => "clear_upcoming" },
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()" %>
    / <%= link_to_remote "clear played tracks", :url => {:controller => "queued_tracks", :action => "clear_played" },
      :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()" %>
      </p>
  <% end %>

  <% reset_cycle %>
<table class="queue">
  <colgroup>
    <col style="width: 3em"/>
    <col style="width: 50%"/>
    <col style="width: 30%"/>
  </colgroup>
  <thead>
    <th></th>
    <th>track</th>
    <th>queued by</th>
  </thead>
  <tbody id="queued_tracks">

  <% @queued_tracks.reverse.each_with_index do |track, i| %>
    <% index = @queued_tracks.size - i - 1  %>
    <% reorderable = index + 1 > @current_track_index %>
    <tr 
      <% if reorderable %>
        style="cursor:move" id="track_<%= track.databaseID %>" 
      <% end %>
      class="<%= cycle('odd', 'even') %> <%= (index + 1 == @current_track_index) ? 'current' : nil %><%= reorderable ? "reorderable" : nil %>">
      <td>
        <small>
        <%= index + 1 %>
        <%= link_to_remote "[x]", :url => { :controller => "queued_tracks", :action => :destroy, :id => index },
          :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"   %>
        </small>
        <br/>
        <%= link_to_remote "pla", :url => { :controller => "queued_tracks", :action => :play_now, :id => index },
          :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
        <%= link_to_remote "reQ", :url => { :controller => "tracks", :action => :queue, :id => track.databaseID },
          :method => :put,
          :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  %>
      </td>
      <td>
        <%= track.artist %>
        <br/><%= track.name %> 
        <br/><%= track.album %>
      </td>
      <td>
        <%= track.comment %>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>
<%= sortable_element("queued_tracks", 
                     :tag => "tr",
                     :only => "reorderable",
                     :url => url_for(:controller => "tracks", :action => "reorder"),
                    :loading => "$('queue-changing').show()", :complete => "$('queue-changing').hide()"  ) %>
</div>
